/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.sortable;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;

import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.JQOptions;
import com.noocodecommit.wicket.stuff.jquery.exception.CannotApplyJQOptionException;
import com.noocodecommit.wicket.stuff.jquery.plugin.IJQPluginComponent;
import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * Component used to design sortables<br>
 * Jquery use for this behavior is <code>$('#id').sortable(options);</code><br>
 * With Wicket, you just need to create a JQSortable Component
 * 
 * @author Benoit Bouchez
 * 
 */
public class JQSortable extends WebMarkupContainer implements IJQPluginComponent
{
	/** JQuery plugin */
	private JQSortablePlugin plugin;
	/** JQuery default options */
	private JQSortableOptions options = new JQSortableOptions();

	// ui.position - current position of the helper
	// ui.absolutePosition - current absolute position of the helper
	// ui.helper - the current helper element (most often a clone of the item)
	// ui.placeholder - the placeholder (if you defined one)

	/** the current dragged element */
	private Component item;
	/** the sortable where the item comes from (only exists if you move from one connected list to another) */
	private JQSortable sender;

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 */
	public JQSortable(String id, String key)
	{
		super(id);
		this.setOutputMarkupId(true);
		if (key == null)
		{
			key = JQSortablePlugin.DEFAULT_KEY;
		}
		if (JQCore.get().getPlugin(key) == null)
		{
			new JQSortablePlugin(key);
		}
		this.plugin = (JQSortablePlugin) JQCore.get().getPlugin(key);
		JQCore.get().getPlugin(plugin.getKey()).attachComponentForDocumentReady(this);
	}

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component id
	 */
	public JQSortable(String id)
	{
		this(id, null);
	}

	public JQSortableOptions getOptions()
	{
		return options;
	}

	public void setOptions(JQOptions options)
	{
		if (options.getClass().equals(JQSortableOptions.class))
		{
			this.options = (JQSortableOptions) options;
		}
		else
		{
			throw new CannotApplyJQOptionException(JQSortable.class, options.getClass(), JQSortableOptions.class);
		}
	}

	public JQSortablePlugin getPlugin()
	{
		return plugin;
	}

	public void setPlugin(JQPlugin plugin)
	{
		this.plugin = (JQSortablePlugin) plugin;
	}

	public String getWidgetReadyScript(boolean reset)
	{
		return "$('#" + this.getMarkupId() + "')." + (reset ? "sortable('destroy')." : "") + options.getDocumentReadyOptions();
	}

	public String getAfterAjaxUpdateScript()
	{
		return "$('#" + this.getMarkupId() + "')." + options.getDocumentReadyOptions();
	}

	/**
	 * Gets the currently sorted item
	 * 
	 * @return the currently sorted component
	 */
	public Component getItem()
	{
		return item;
	}

	/**
	 * Sets the currently sorted item
	 * 
	 * @param item
	 *            the currently sorted component
	 */
	public void setItem(Component item)
	{
		this.item = item;
	}

	/**
	 * Gets the current sender
	 * 
	 * @return the current sending component
	 */
	public JQSortable getSender()
	{
		return sender;
	}

	/**
	 * Sets the current sender
	 * 
	 * @param sender
	 *            the current sending component
	 */
	public void setSender(JQSortable sender)
	{
		this.sender = sender;
	}

	/**
	 * Enables this sortable<br>
	 * Appends a javascript call <code>$('#id').sortable('enable')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void enable(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('enable')");
	}

	/**
	 * Disables this sortable<br>
	 * Appends a javascript call <code>$('#id').sortable('disable')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void disable(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('disable')");
	}

	/**
	 * Destroys this sortable<br>
	 * Appends a javascript call <code>$('#id').sortable('destroy')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void destroy(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('destroy')");
	}

	/**
	 * Serializes this sortable<br>
	 * Appends a javascript call <code>$('#id').sortable('serialize', {attribute, expression, key})</code> to the ajax target.
	 * 
	 * @param attribute
	 *            serialize attributes
	 * @param expression
	 *            serialize expression
	 * @param key
	 *            serialize key
	 * @param target
	 *            Ajax target
	 */
	public void serialize(String attribute, String expression, String key, AjaxRequestTarget target)
	{
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		if (attribute != null)
		{
			sb.append("attribute:'" + attribute + "',");
		}
		if (expression != null)
		{
			sb.append("expression:'" + expression + "',");
		}
		if (key != null)
		{
			sb.append("key:'" + key + "'");
		}
		sb.append("}");
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('serialize'," + sb.toString() + ")");
	}

	/**
	 * Refresh this sortable<br>
	 * Appends a javascript call <code>$('#id').sortable('refresh')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void refresh(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('refresh')");
	}

	/**
	 * Refresh this sortable positions<br>
	 * Appends a javascript call <code>$('#id').sortable('refreshPositions')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void refreshPositions(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('refreshPositions')");
	}
}
